package haupt.dev.NeocronServiceQuery;

import org.pmw.tinylog.Logger;

import haupt.dev.Config.ConfigFileReader;
import haupt.dev.Config.ConfigReader;
import haupt.dev.DatabaseConnection.MySQLConnection;

public class NeocronServiceQuery implements Runnable {
	
	private String token;
	private MySQLConnection dbConnection;
	private ConfigReader CFGreader;
	
	public static void main(String[] args) {
		Thread th = new Thread(new NeocronServiceQuery());
		th.start();
	}

	@Override
	public void run() {
		CFGreader = new ConfigFileReader();
		Logger.info("up and running...");
		while(true) {
			Logger.info("try to connect to database and get session token");
			dbConnection = new MySQLConnection(CFGreader);
			token = AccountManager.getSessionToken();
			
			Logger.info("get data from SOAP Service");
			dbConnection.storeServerData(InterfaceManager.getServerStatistic(token));
			dbConnection.storeOutpostData(InterfaceManager.getExtendedOutposts(token, CFGreader.getProperty("server")));
			
			Logger.info("try to close database connection and let the thread sleep");
			dbConnection.close();
			dbConnection = null;
			tryThreadStateSleep();
		}
	}

	private void tryThreadStateSleep() {
		try {
			Thread.sleep(600000);
		} catch(InterruptedException e) {
			Logger.error("Thread interrupted. Please restart the process...");
		}
	}
}
